package com.crl.ssc.dic.dao;

import com.cars.util.page.PageUtil;
import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.ssc.dic.model.DicDistrict;
import com.crl.ssc.dic.vo.DicDistrictSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @description: 字典管理-区县字典Dao
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020-07-26 14:44:13
 */
@Repository
public class DicDistrictDao extends BaseDao<DicDistrict, DicDistrictSearchVO> {
    /**
     * 新增
     *
     * @param dicDistrict
     * @return
     */
    public int add(DicDistrict dicDistrict) {
        String sql = "INSERT INTO td_dic_distict(city_code,district_code,district_name) " +
                "VALUES(:cityCode,:districtCode,:districtName)";
        return insert(sql, dicDistrict);
    }

    /**
     * 修改
     *
     * @param dicDistrict
     * @return
     */
    public int update(DicDistrict dicDistrict) {
        String sql = "UPDATE td_dic_distict SET city_code=:cityCode,district_name=:districtName " +
                    "WHERE district_code=districtCode";
        return update(sql, dicDistrict);
    }

    /**
     * 删除
     *
     * @param districtCode
     * @return
     */
    public int delete(String districtCode) {
        String sql = "DELETE FROM td_dic_distict WHERE district_code=?";
        return delete(sql, districtCode);
    }
    /**
     * 列表
     *
     * @param dicDistrictSearchVO
     * @return
     */
    public List<DicDistrict> list(DicDistrictSearchVO dicDistrictSearchVO) {
        String sql = "SELECT  FROM td_dic_distict WHERE 1=1 ";
        sql += createSearchSQL(dicDistrictSearchVO);
        sql += " ORDER BY district_code ASC";
        sql = PageUtil.createOraclePageSQL(sql, dicDistrictSearchVO.getPage());
        return list(sql, dicDistrictSearchVO);
    }
    /**
     * 查询数据条数
     *
     * @param dicDistrictSearchVO
     * @return
     */
    public int count(DicDistrictSearchVO dicDistrictSearchVO) {
        String sql = "SELECT COUNT(district_code) FROM td_dic_distict WHERE 1=1 ";
        sql += createSearchSQL(dicDistrictSearchVO);
        return count(sql, dicDistrictSearchVO);
    }
    /**
       * 拼接查询条件
       *
       * @param dicDistrictSearchVO
       * @return
    */
    private String createSearchSQL(DicDistrictSearchVO dicDistrictSearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(dicDistrictSearchVO.getCityCode())) {
			sql += " AND city_code =:cityCode ";
		}if (StringUtil.isNotNullOrEmpty(dicDistrictSearchVO.getDistrictCode())) {
			sql += " AND district_code =:districtCode ";
		}if (StringUtil.isNotNullOrEmpty(dicDistrictSearchVO.getDistrictName())) {
			sql += " AND district_name =:districtName ";
		}
        return sql;
    }

    /**
     * 获取单个
     *
     * @param districtCode
     * @return
     */
    public DicDistrict get(String districtCode) {
        String sql = "SELECT city_code,district_code,district_name FROM td_dic_distict WHERE district_code=?";
        return get(sql, districtCode);
    }
}
